package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.model.NotificationModel;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.ui.adapter.InfoNotifyAdapter;
import keeto.com.vn.bms.ui.widget.DialogDeleteNotify;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ViewMoreNotifyActivity extends Activity implements OnClickListener, OnRefreshListener<ListView> {
	private final int PAGE_LIMIT = 10;

	private PullToRefreshListView listPulltoRefresh;

	private ImageButton btnBack;
	private TextView tvTitleName;
	private ImageButton btnLogon;
	private ImageButton btnDelete;
	private BMSSharedPreferences preferences;
	private int PageNumber = 1;
	private ViewMoreListener listener = new ViewMoreListener();
	private ArrayList<NotificationModel> mArrayList = new  ArrayList<NotificationModel>();
	private InfoNotifyAdapter mAdapter;
	private StringBuilder  builder;

	public static void actionLaunch(Context context){
		Intent intent= new Intent(context,ViewMoreNotifyActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_list_infor);
		preferences = new BMSSharedPreferences(this);
		initViewComponent();
	}

	private void initViewComponent() {
		btnBack = (ImageButton)findViewById(R.id.btn_tittle_back);
		tvTitleName = (TextView) findViewById(R.id.tv_title_name);
		btnLogon = (ImageButton)findViewById(R.id.btn_tittle_login);
		btnDelete = (ImageButton)findViewById(R.id.btn_tittle_delete);
		btnLogon.setVisibility(View.GONE);
		btnDelete.setVisibility(View.VISIBLE);
		listPulltoRefresh = (PullToRefreshListView) findViewById(R.id.lst_product);

		tvTitleName.setText(getString(R.string.info_notify));

		btnBack.setOnClickListener(this);
		btnDelete.setOnClickListener(this);
		listPulltoRefresh.setOnRefreshListener(this);
		mAdapter = new InfoNotifyAdapter(this, mArrayList,listener);
		listPulltoRefresh.setAdapter(mAdapter);

		BMSCore.getInstance(ViewMoreNotifyActivity.this).taskGetListNotify(preferences.getUserAuthKey(this), PAGE_LIMIT, PageNumber, listener);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_tittle_back:
			this.finish();
			break;
		case R.id.btn_tittle_delete:
			deleteNofity();
			break;

		default:
			break;
		}
	}

	private void deleteNofity(){
		builder = new StringBuilder();
		builder.append("[");
		for(int i=0;i<mArrayList.size();i++){
			NotificationModel model =  mArrayList.get(i);
			if (model.isChecked() == true){
				builder.append("\"");
				builder.append( model.getNotifyId());
				builder.append("\",");
			}
			
		}
		String ListId = builder.toString().substring(0,builder.length()-1) +"]";
		DialogDeleteNotify deleteNotify = new DialogDeleteNotify(ViewMoreNotifyActivity.this, listener, ListId);
		deleteNotify.show();

	}

	class ViewMoreListener extends BMSListener{
		@Override
		public void finishGetListNotification(final int returnCode, final int pageCount,
				final ArrayList<NotificationModel> arrayList) {
			// TODO Auto-generated method stub
			super.finishGetListNotification(returnCode, pageCount, arrayList);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					listPulltoRefresh.onRefreshComplete();
					if (returnCode == IBMSProtocol.STATUS_CODE_OK && arrayList != null){
						if (PageNumber < pageCount){
							PageNumber += 1;
						}else{
							PageNumber = -1;
						}
						mArrayList.addAll(arrayList);
						mAdapter.notifyDataSetChanged();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ViewMoreNotifyActivity.this, ViewMoreNotifyActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(ViewMoreNotifyActivity.this, ViewMoreNotifyActivity.this.getResources().getString(R.string.error_data_server), Toast.LENGTH_SHORT).show();
					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ViewMoreNotifyActivity.this, ViewMoreNotifyActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		@Override
		public void finishSendDeleteNotify(final int returnCode, final String resultMsg) {
			// TODO Auto-generated method stub
			super.finishSendDeleteNotify(returnCode, resultMsg);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (returnCode == IBMSProtocol.STATUS_CODE_OK ){
						mArrayList.clear();
						mAdapter.notifyDataSetChanged();
						PageNumber = 1;
						BMSCore.getInstance(ViewMoreNotifyActivity.this).taskGetListNotify(preferences.getUserAuthKey(ViewMoreNotifyActivity.this), PAGE_LIMIT, PageNumber, listener);

					}else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(ViewMoreNotifyActivity.this, ViewMoreNotifyActivity.this.getResources().getString(R.string.error_network_connection), Toast.LENGTH_SHORT).show();
					}
					else if (returnCode == IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(ViewMoreNotifyActivity.this, ViewMoreNotifyActivity.this.getResources().getString(R.string.error_perpose), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		if (PageNumber>0){
			BMSCore.getInstance(ViewMoreNotifyActivity.this).taskGetListNotify(preferences.getUserAuthKey(this), PAGE_LIMIT, PageNumber, listener);
		}else{
			new Handler().post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					listPulltoRefresh.onRefreshComplete();

				}
			});


		}
	}

}
